home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / commodities / HotKey.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  10KB  |  245 lines

  1. ;/* HotKey.c - Simple hot key commodity compiled with SASC 5.10
  2. LC -b0 -cfist -v -j73 hotkey.c
  3. Blink FROM LIB:c.o,hotkey.o TO hotkey LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33.  
  34. #include <exec/libraries.h>
  35. #include <libraries/commodities.h>
  36. #include <dos/dos.h>
  37. #include <clib/exec_protos.h>
  38. #include <clib/alib_protos.h>
  39. #include <clib/alib_stdio_protos.h>
  40. #include <clib/commodities_protos.h>
  41.  
  42. #ifdef LATTICE
  43. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  44. int chkabort(void) { return(0); }
  45. #endif
  46.  
  47. #define EVT_HOTKEY 1L
  48.  
  49. void main(int, char **);
  50. void ProcessMsg(void);
  51.  
  52. struct Library *CxBase, *IconBase;
  53. struct MsgPort *broker_mp;
  54. CxObj *broker, *filter, *sender, *translate;
  55.  
  56. struct NewBroker newbroker = {
  57.     NB_VERSION,
  58.     "RKM HotKey",           /* string to identify this broker */
  59.     "A Simple HotKey",
  60.     "A simple hot key commodity",
  61.     NBU_UNIQUE | NBU_NOTIFY,    /* Don't want any new commodities starting with this name. */
  62.     0, 0, 0, 0                  /* If someone tries it, let me know */
  63. };
  64.  
  65. ULONG cxsigflag;
  66.  
  67. void main(int argc, char **argv)
  68. {
  69.     UBYTE *hotkey, **ttypes;
  70.     CxMsg *msg;
  71.  
  72.     if (CxBase = OpenLibrary("commodities.library", 37L))
  73.     {
  74.         /* open the icon.library for the support library */
  75.         /* functions, ArgArrayInit() and ArgArrayDone()  */
  76.         if (IconBase = OpenLibrary("icon.library", 36L))
  77.         {
  78.             if (broker_mp = CreateMsgPort())
  79.             {
  80.                 newbroker.nb_Port = broker_mp;
  81.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  82.  
  83.                 /* ArgArrayInit() is a support library function (from the 2.0 version
  84.                  * of amiga.lib) that makes it easy to read arguments from either a
  85.                  * CLI or from Workbench's ToolTypes.  Because it uses icon.library,
  86.                  * the library has to be open before calling this function.
  87.                  * ArgArrayDone() cleans up after this function.
  88.                  */
  89.                 ttypes = ArgArrayInit(argc, argv);
  90.  
  91.                 /* ArgInt() (also from amiga.lib) searches through the array set up
  92.                  * by ArgArrayInit() for a specific ToolType.  If it finds one, it
  93.                  * returns the numeric value of the number that followed the
  94.                  * ToolType (i.e., CX_PRIORITY=7). If it doesn't find the ToolType,
  95.                  * it returns the default value (the third argument)
  96.                  */
  97.                 newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  98.  
  99.                 /* ArgString() works just like ArgInt(), except it returns a pointer to a string
  100.                  * rather than an integer. In the example below, if there is no ToolType
  101.                  * "HOTKEY", the function returns a pointer to "rawkey control esc".
  102.                  */
  103.                 hotkey = ArgString(ttypes, "HOTKEY", "rawkey control esc");
  104.  
  105.                 if (broker = CxBroker(&newbroker, NULL))
  106.                 {
  107.                     /* CxFilter() is a macro that creates a filter CxObject.  This filter
  108.                      * passes input events that match the string pointed to by hotkey.
  109.                      */
  110.                     if (filter = CxFilter(hotkey))
  111.                     {
  112.                         /* Add a CxObject to another's personal list */
  113.                         AttachCxObj(broker, filter);
  114.  
  115.                         /* CxSender() creates a sender CxObject.  Every time a sender gets
  116.                          * a CxMessage, it sends a new CxMessage to the port pointed to in
  117.                          * the first argument. CxSender()'s second argument will be the ID
  118.                          * of any CxMessages the sender sends to the port.  The data pointer
  119.                          * associated with the CxMessage will point to a *COPY* of the
  120.                          * InputEvent structure associated with the orginal CxMessage.
  121.                          */
  122.                         if (sender = CxSender(broker_mp, EVT_HOTKEY))
  123.                         {
  124.                             AttachCxObj(filter, sender);
  125.  
  126.                             /* CxTranslate() creates a translate CxObject. When a translate
  127.                              * CxObject gets a CxMessage, it deletes the original CxMessage
  128.                              * and adds a new input event to the input.device's input stream
  129.                              * after the Commodities input handler. CxTranslate's argument
  130.                              * points to an InputEvent structure from which to create the new
  131.                              * input event.  In this example, the pointer is NULL, meaning no
  132.                              * new event should be introduced, which causes any event that
  133.                              * reaches this object to disappear from the input stream.
  134.                              */
  135.                             if (translate = (CxTranslate(NULL)))
  136.                             {
  137.                                 AttachCxObj(filter, translate);
  138.  
  139.                                 /* CxObjError() is a commodities.library function that returns
  140.                                  * the internal accumulated error code of a CxObject.
  141.                                  */
  142.                                 if (! CxObjError(filter))
  143.                                 {
  144.                                     ActivateCxObj(broker, 1L);
  145.                                     ProcessMsg();
  146.                                 }
  147.                             }
  148.                         }
  149.                     }
  150.                     /* DeleteCxObjAll() is a commodities.library function that not only
  151.                      * deletes the CxObject pointed to in its argument, but it deletes
  152.                      * all of the CxObjects that are attached to it.
  153.                      */
  154.                     DeleteCxObjAll(broker);
  155.  
  156.                     /* Empty the port of all CxMsgs */
  157.                     while(msg = (CxMsg *)GetMsg(broker_mp))
  158.                         ReplyMsg((struct Message *)msg);
  159.                 }
  160.                 DeletePort(broker_mp);
  161.             }
  162.             /* this amiga.lib function cleans up after ArgArrayInit() */
  163.             ArgArrayDone();
  164.             CloseLibrary(IconBase);
  165.         }
  166.         CloseLibrary(CxBase);
  167.     }
  168. }
  169. void ProcessMsg(void)
  170. {
  171.     extern struct MsgPort *broker_mp;
  172.     extern CxObj *broker;
  173.     extern ULONG cxsigflag;
  174.     CxMsg *msg;
  175.     ULONG sigrcvd, msgid, msgtype;
  176.     LONG returnvalue = 1L;
  177.  
  178.     while(returnvalue)
  179.     {
  180.         sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  181.  
  182.         while(msg = (CxMsg *)GetMsg(broker_mp))
  183.         {
  184.             msgid = CxMsgID(msg);
  185.             msgtype = CxMsgType(msg);
  186.             ReplyMsg((struct Message *)msg);
  187.  
  188.             switch(msgtype)
  189.             {
  190.                 case CXM_IEVENT:
  191.                     printf("A CXM_EVENT, ");
  192.                     switch(msgid)
  193.                     {
  194.                         case EVT_HOTKEY: /* We got the message from the sender CxObject */
  195.                             printf("You hit the HotKey.\n");
  196.                             break;
  197.                         default:
  198.                             printf("unknown.\n");
  199.                             break;
  200.                     }
  201.                     break;
  202.                 case CXM_COMMAND:
  203.                     printf("A command: ");
  204.                     switch(msgid)
  205.                     {
  206.                         case CXCMD_DISABLE:
  207.                             printf("CXCMD_DISABLE\n");
  208.                             ActivateCxObj(broker, 0L);
  209.                             break;
  210.                         case CXCMD_ENABLE:
  211.                             printf("CXCMD_ENABLE\n");
  212.                             ActivateCxObj(broker, 1L);
  213.                             break;
  214.                         case CXCMD_KILL:
  215.                             printf("CXCMD_KILL\n");
  216.                             returnvalue = 0L;
  217.                             break;
  218.                         case CXCMD_UNIQUE:
  219.                         /* Commodities Exchange can be told not only to refuse to launch a
  220.                          * commodity with a name already in use but also can notify the
  221.                          * already running commodity that it happened. It does this by
  222.                          * sending a CXM_COMMAND with the ID set to CXMCMD_UNIQUE.  If the
  223.                          * user tries to run a windowless commodity that is already running,
  224.                          * the user wants the commodity to shut down. */
  225.                             printf("CXCMD_UNIQUE\n");
  226.                             returnvalue = 0L;
  227.                             break;
  228.                         default:
  229.                             printf("Unknown msgid\n");
  230.                             break;
  231.                     }
  232.                     break;
  233.                 default:
  234.                     printf("Unknown msgtype\n");
  235.                     break;
  236.             }
  237.         }
  238.         if (sigrcvd & SIGBREAKF_CTRL_C)
  239.         {
  240.             returnvalue = 0L;
  241.             printf("CTRL C signal break\n");
  242.         }
  243.     }
  244. }
  245.